package com.qfmy.service.impl.common;

import com.qfmy.mapper.common.UserFeedbackMapper;
import com.qfmy.service.common.UserFeedbackService;
import common.exception.BaseException;
import common.login.LoginUser;
import common.login.LoginUserHolder;
import common.result.ResultCodeEnum;
import model.entity.common.UserFeedback;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 清风明月
 * @Package com.qfmy.service.impl.common
 * @date 2025/5/22
 * @description 意见反馈
 */
@Service
@SuppressWarnings("all")
public class UserFeedbackServiceImpl implements UserFeedbackService {

    /**
     * 意见反馈mapper
     */
    @Autowired
    private UserFeedbackMapper userFeedbackMapper;


    /**
     * 意见反馈
     * @param content
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void feedback(@NotNull UserFeedback userFeedback) {
        //获取当前用户id
        Long userId = getUserId();
        userFeedback.setUserId(userId);
        userFeedbackMapper.insertUserFeedback(userFeedback);
    }

    /**
     * 获取当前用户id
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断登录用户是否为空
        if (loginUser == null) {
            throw  new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        return loginUser.getUserId();
    }
}
